<script>
	import { translatePrice } from '@/utils/TranslatePriceC/index.js'
	import { NAVIGATE } from '@/config/postMessage-types.js'

	export default {
		data() {
			return {
				buttonType: 2,
				city: "", //产地
				enterpriseName: "宁家", // 商家名称
				eachPrice: 0,
				featureLabelList: null, // 类名查询表
				featureClass: '', //特色标签css类名
				featureLabel: 0, //特色标签
				featureName: '',
				goodsCode: "", //产品分类
				goodsId: "", //产品ID
				goodsName: "", //产品名称
				goodsPrice: "", //产品价格
				goodsUnit: "", //产品单位
				goodsModel: "", // 商品型号
				goodsType: 0, //售价
				goodsDeliveryType: '', //商品类型大件常规
				height: 0, //外形高度
				isShow: true, // 是否展示
				inActivity: 0,
				regularTypeLabel: '特价', // 优惠类型标签
				labellistFir: '', //标签1
				labellistSec: '', //标签2
				labellistThir: '', //标签3
				labellistFor: '', //标签4
				mainPic: '', //主图缩略图
				normalPrice: "1.00", //原价
				shopName: "", //商店名称
				saleqty: "", //库存
				subTitle: '', // 二级标题
				showWsPrice: 0, // 展示用的价格
				showNormalPrice: 0,
				showUnit: '', //展示用的规格
				regularLabel: [],
				titleList: {
					color: 'black',
					fontSize: '0.7rem',
					fonsWeight: '400',
				},
				labelStyle: {},
				titleStyle: {},
				subTitleStyle: {},
				buttonStyle: {
					backgroundColor: '',
					color: '#EF5423'
				}
				
			}
		},
		props: {
			comInfor: {
				type: Object,
				default: () => ({}),
			},
			styleConfig: {
				itemStyle: {
					'font-size': ''
				},
				goodsPriceStyle: {},
				normalPriceStyle: {},
				labelStyle: {},
				titleStyle: {},
				subTitleStyle: {},
				buttonStyle: {
					backgroundColor: '',
					color: '#EF5423'
				},

			},
			buttonActive: {

				default: null

			},
			isOpacity: {
				type: Boolean,
				default: false
			},
			wrpperHeight: {
				type: Number,
				default: 0
			},
			wrpperTop: {
				type: Number,
				default: 0
			},
			goMoreFn: {
				default: null
			},
			isLastItem: {
				type: Boolean,
				default: false
			}
		},
		methods: {
			translatePrice: translatePrice,
			goMore() {
				this.goMoreFn && this.goMoreFn()
			},
			changePrice(data) {
				return parseFloat(data).toFixed(2)
			},
			initData() {
				var comInfor = this.comInfor
				this.goodsCode = comInfor.goodsCode //产品分类
				this.enterpriseName = comInfor.enterpriseName // 商家名称
				this.goodsId = parseInt(comInfor.goodsId) //产品ID
				this.goodsName = comInfor.goodsName //产品名称
				this.goodsPrice = comInfor.goodsPrice //产品价格
				this.goodsUnit = comInfor.goodsUnit //产品单位
				this.city = comInfor.city //产地
				this.shopName = comInfor.shopName //商店名称
				this.saleqty = comInfor.saleqty //库存
				this.goodsModel = comInfor.goodsModel // 商品型号
				this.goodsType = comInfor.goodsType //售价
				this.normalPrice = comInfor.normalPrice //原价
				this.mainPic = comInfor.mainPic //主图缩略图
				this.subTitle = comInfor.subTitle //二级标题
				this.showUnit = comInfor.showUnit
				this.showWsPrice = comInfor.showWsPrice
				this.inActivity = comInfor.inActivity
				this.goodsDeliveryType = comInfor.goodsDeliveryType
				this.labellistFir = comInfor.labellistFir
				this.labellistSec = comInfor.labellistSec
				this.labellistThir = comInfor.labellistThir
				this.eachPrice = comInfor.eachPrice
				this.labellistFor = comInfor.labellistFor
				this.regularTypeLabel = comInfor.regularTypeLabel
				this.showNormalPrice = comInfor.showNormalPrice
				this.lastStockCountNum = comInfor.lastStockCountNum || 0;
				this.regularLabel = comInfor.regularLabel
				if(this.lastStockCountNum <= 0) {
					this.buttonStyle.color = 'gray'
				}else{
					this.buttonStyle.color = '#EF5423'
				}
			},
			gotoDetail() {
				if(window.isApp) {
					let goBackIns = {
						type: NAVIGATE,
						params: {
							screen: 'Detail',
							params: {
								goodsCode: this.goodsCode
							}
						}
					}
					window.postMessage(JSON.stringify(goBackIns));
				} else {
					this.$router.push({
						path: 'detail',
						query: {
							code: this.goodsCode
						}
					})
				}

			},

			changStyle() {
				if(!this.isOpacity) {
					return true
				}
				var obj = this.$refs.listItem
				var itemTop = obj ? obj.getBoundingClientRect().top - this.wrpperTop : 0
				var height = this.height
				if(itemTop < this.wrpperHeight - 3 && itemTop + height > 0) {
					this.isShow = true
				} else {
					this.isShow = false
				}
			},

			cartButtonOnClick() {
				if(this.isShare) {
					this.$router.push({
						name: "MiddleWare",
					})
					return
				}
				if(this.lastStockCountNum <= 0){
					return
				}else{
					this.gotoDetail();
				}
				// if(this.buttonActive) {
				// 	this.buttonActive()
				// } else {
				// 	var updateCart = this.$refs.updateCart
				// 	updateCart && updateCart.addToCart()
				// }
			}

		},
		computed: {
			pagePosY() {
				var post = this.$store.state.cjy.pagePosY
				return post
			},
			isShare() {
				let share = this.$route.query.share;
				if(share) {
					return true;
				} else {
					return false
				}
			}
		},
		watch: {
			pagePosY(data) {
				this.changStyle()
			},
			comInfor () {
				this.initData();
			}
		},
		created() {
			this.initData();
		},
		mounted() {
			this.$nextTick(() => {
				var obj = this.$refs.listItem
				this.height = obj ? obj.offsetHeight : 0
				this.changStyle()
			})
		},
		updated() {
			this.changStyle()
		},
	}
</script>

<style>
	.cjy-page-image-box-img {
		position: absolute;
		width: 100%;
		height: 100%;
	}
</style>